package com.manage.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class SpringApplicationContextHolder
		implements ApplicationContextAware {

	private static Logger logger = LoggerFactory.getLogger(SpringApplicationContextHolder.class);
	private static ApplicationContext context;

	@Override
	public void setApplicationContext(ApplicationContext context) throws BeansException {
		logger.info("setApplicationContext called");
		SpringApplicationContextHolder.context = context;
	}

	public static Object getSpringBean(String beanName) {
		return context == null ? null : context.getBean(beanName);
	}

	public static String[] getBeanDefinitionNames() {
		return context.getBeanDefinitionNames();
	}

	public static <T> T getSpringBeanByType(Class<T> clazz) {
		return context == null ? null : context.getBean(clazz);
	}
}
